package com.nuoyuan.wx.service.helper;

import com.nuoyuan.wx.vo.ProductVo;
import com.nuoyuan.wx.common.utils.DateUtil;
import com.nuoyuan.wx.dao.po.ProductPo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Package com.nuoyuan.wx.service.helper
 * @ClassName ProductBeanHelper
 * @Description TODO
 * @Author linus
 * @Date 2020-02-16 15:00
 * @Version 1.0
 **/
public class ProductBeanHelper {

    public static ProductPo toPo(ProductVo vo) {
        ProductPo po = new ProductPo();

        Timestamp current = DateUtil.getCurrentTimestamp();
        po.setAnnualYield(vo.getAnnualYield());
        po.setBackupFileUrl(vo.getBackupFileUrl());
        po.setContent(vo.getContent());
        po.setCreateTime(current);
        po.setDescFileUrl(vo.getDescFileUrl());
        po.setExpireationMonth(vo.getExpireationMonth());
        po.setIsDeleted(vo.getIsDeleted());
        po.setIsSale(vo.getIsSale());
        po.setOtherFileUrl(vo.getOtherFileUrl());
        po.setPaymentType(vo.getPaymentType());
        po.setProductId(vo.getProductId());
        po.setProductName(vo.getProductName());
        po.setProductNo(vo.getProductNo());
        po.setPublisher(vo.getPublisher());
        po.setPublishType(vo.getPublishType());
        po.setRiskFileUrl(vo.getRiskFileUrl());
        po.setStartAmount(vo.getStartAmount());
        po.setTotalCollection(vo.getTotalCollection());
        po.setUpdateTime(current);
        po.setIfEnd(vo.getIfEnd());
        return po;
    }

    public static ProductVo toVo(ProductPo po) {
        ProductVo vo = new ProductVo();

        vo.setAnnualYield(po.getAnnualYield());
        vo.setBackupFileUrl(po.getBackupFileUrl());
        vo.setContent(po.getContent());
        vo.setDescFileUrl(po.getDescFileUrl());
        vo.setExpireationMonth(po.getExpireationMonth());
        vo.setIsDeleted(po.getIsDeleted());
        vo.setIsSale(po.getIsSale());
        vo.setOtherFileUrl(po.getOtherFileUrl());
        vo.setPaymentType(po.getPaymentType());
        vo.setProductId(po.getProductId());
        vo.setProductName(po.getProductName());
        vo.setProductNo(po.getProductNo());
        vo.setPublisher(po.getPublisher());
        vo.setPublishType(po.getPublishType());
        vo.setRiskFileUrl(po.getRiskFileUrl());
        vo.setStartAmount(po.getStartAmount());
        vo.setTotalCollection(po.getTotalCollection());
        vo.setUpdateTime(DateUtil.formatDate(po.getUpdateTime(), DateUtil.FMT_DATE_YYYYMMDD_HHMMSS));
        vo.setCreateTime(DateUtil.formatDate(po.getCreateTime(), DateUtil.FMT_DATE_YYYYMMDD_HHMMSS));
        vo.setIfEnd(po.getIfEnd());
        return vo;
    }

    public static List<ProductVo> toVos(List<ProductPo> pos) {
        if(CollectionUtils.isEmpty(pos)) {
            return null;
        }
        List<ProductVo> vos = new ArrayList<>();
        for(ProductPo po : pos) {
            ProductVo vo = toVo(po);
            vos.add(vo);
        }
        return vos;
    }
}
